﻿using System;
using System.Windows;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;
using P110_BaseMotion;
using System.Windows.Media.Imaging;
using System.IO;
using System.Runtime.InteropServices;

namespace GTSDemo.Views
{
    [ValueConversion(typeof(bool), typeof(Color))]
    internal class ActiveColorConverter : IValueConverter
    {
        //<Setter.Value>
        //    <RadialGradientBrush Center = "0.3,0.3" >
        //        < GradientStop Color="#AA57A64A"
        //                      Offset="0.1" />
        //        <GradientStop Color = "#FF57A64A"
        //                      Offset="1" />
        //    </RadialGradientBrush>
        //</Setter.Value>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool date = (bool)value;
            if (Application.Current == null) return null;
            RadialGradientBrush retBrush = new RadialGradientBrush();
            if (date)
            {
                retBrush = (RadialGradientBrush)Application.Current.FindResource("InputOn");
                ////FF57A64A
                //retBrush.Center = new Point(0.5, 0.5);
                //retBrush.GradientStops.Add(new GradientStop(Color.FromArgb(0xAA, 0x57, 0xA6, 0x4A), 0.1));
                //retBrush.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0x57, 0xA6, 0x4A), 1));
            }
            else
            {
                retBrush = (RadialGradientBrush)Application.Current.FindResource("InputOff");
                //retBrush.Center = new Point(0.5, 0.5);
                //retBrush.GradientStops.Add(new GradientStop(Color.FromArgb(0xFF, 0x20, 0x00, 0x00), 1));
                //retBrush.GradientStops.Add(new GradientStop(Color.FromArgb(0x50, 0xFF, 0x00, 0x00), 0));
            }
            return retBrush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException("Please binding in one-way.");
        }
    }
    [ValueConversion(typeof(HomeMode), typeof(int))]
    internal class HomeModeIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (HomeMode)value;
        }
    }
    [ValueConversion(typeof(ControlMode), typeof(int))]
    internal class ControlModeIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (ControlMode)value;
        }
    }
    [ValueConversion(typeof(MotorType), typeof(int))]
    internal class MotorTypeIntConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (int)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (MotorType)value;
        }
    }
    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class BoolVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((bool)value)
            {
                return Visibility.Visible;
            }
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((Visibility)value == Visibility.Visible)
                return true;
            else return false;
        }
    }
    [ValueConversion(typeof(P000_Base.ManagerMode), typeof(bool))]
    public class HideProgramUserConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            P000_Base.ManagerMode managerMode = (P000_Base.ManagerMode)value;
            //if (managerMode == P000_Base.ManagerMode.Programer) return Visibility.Collapsed;
            //else return Visibility.Visible;
            return managerMode == P000_Base.ManagerMode.Programer;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    [ValueConversion(typeof(byte[]), typeof(ImageSource))]
    public class BytesImageSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var bytes = value as byte[];
            if (value == null) return null;
            BitmapImage img = new BitmapImage();
      
            img.CreateOptions = BitmapCreateOptions.PreservePixelFormat;
            img.CacheOption = BitmapCacheOption.OnLoad;
            using (MemoryStream ms = new MemoryStream(bytes))
            {
                ms.Seek(0, SeekOrigin.Begin);
                img.BeginInit();
                img.StreamSource = ms;
                img.EndInit();
            }
            return img as ImageSource;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


}
